package com.biz.service.MService.comment;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.biz.service.HService.base.BaseServiceImpl;
import com.framework.dao.mdao.DaoSupport;
import com.framework.util.Tools;

import org.springframework.stereotype.Service;

import com.biz.model.Hmodel.base.Tpics;
import com.biz.model.Pmodel.comment.Pscore;
import com.biz.model.Pmodel.comment.PscoreDetail;
import com.biz.model.Pmodel.comment.PscoreProject;
import com.biz.model.Pmodel.comment.PscoreText;

import javax.annotation.Resource;

/**
 * **********************************************
 *
 * @author LZQ
 *         <p>
 *         ***********************************************
 */
@SuppressWarnings("unchecked")
@Service("scoreServiceImpl")
public class ScoreServiceImpl extends BaseServiceImpl implements ScoreServiceI {

    @SuppressWarnings("rawtypes")
    @Resource(name = "daoSupport")
    private DaoSupport dao;

  //需要 打分项 (isbook ：订单类型)
    @Override
    public List<PscoreProject> queryScoreProjectNew()
            throws Exception {
        List<PscoreProject> rmap = (List<PscoreProject>) dao.findForList("ScoreDao.getScoreProjectNew", null);
        return rmap;
    }
    
    
  //评论已经有几条记录，用于判断是否首次评价
    @Override
    public Integer queryIsPL(String goodsid, String orderDetailId) throws Exception {
        Map<String, Object> selectmap = new HashMap<String, Object>();
        selectmap.put("goods_id", goodsid);
        selectmap.put("order_detail_id", orderDetailId);
        Integer i = (Integer) dao.findForObject("ScoreDao.getScoreCount", selectmap);
        return i;
    }
    
    
  //插入(主表，打分表，文字评价，图片)
    public boolean saveScoreAll(Pscore add_Pscore, List<PscoreDetail> addList_PscoreDetail, PscoreText add_PscoreText, List<Tpics> picsList) throws Exception {

        boolean a = this.saveScore(add_Pscore, addList_PscoreDetail);
        boolean b = this.saveScoreText(add_PscoreText);
        boolean c = this.updatePic(picsList);

        //修改订单表状态为已评论
        Map<String, Object> reutnmap = new HashMap<String, Object>();
        reutnmap.put("order_detail_id", add_Pscore.getOrder_detail_id());
        reutnmap.put("isScore", 1);
        Integer j = (Integer) dao.save("ScoreDao.updateOrderDetail", reutnmap);

        return true;
    }
    
  //插入评论（主表，打分表）
    @Override
    public boolean saveScore(Pscore new_score, List<PscoreDetail> new_detail) throws Exception {
        //评论主表
        Integer i = (Integer) dao.save("ScoreDao.insertOrderScore", new_score);

        //评论分数
        boolean pd = true;
        Integer k = 0;
        for (PscoreDetail detail : new_detail) {
            k = (Integer) dao.save("ScoreDao.insertOrderScoreDetail", detail);
            if (k == 0) {
                pd = false;
            }
        }

        if (!pd || (i == 0)) {
            //有保存失败数据，回滚
            Double sss = Double.parseDouble("错误");
        }

        return true;

    }
    
  //插入评论（文字评价表）
    @Override
    public boolean saveScoreText(PscoreText new_text) throws Exception {
        //评论文字表
        Integer j = 0;
        if (new_text != null) {
            if (Tools.isEmpty(new_text.getInfo()) && (!new_text.isIsfile())) {
                j = 1;
            } else {
                j = (Integer) dao.save("ScoreDao.insertOrderScoreText", new_text);
            }
        } else {
            j = 1;
        }
        return j > 0;
    }
    
  //修改图片
    @Override
    public boolean updatePic(List<Tpics> picsList) throws Exception {
        boolean pd = true;
        Integer k = 0;
        String ids = "";
        String mapid = "";
        for (Tpics pics : picsList) {
            ids += "'" + pics.getId() + "',";
            mapid = pics.getMainId();
        }
        if (Tools.isEmpty(ids)) {
            k = 1;
        } else {
            ids = ids.substring(0, ids.length() - 1);
            Tpics new_Tpics = new Tpics();
            new_Tpics.setId(ids);
            new_Tpics.setMainId(mapid);
            k = (Integer) dao.update("ScoreDao.updatePic", new_Tpics);
        }

        if (k == 0) {
            //有保存失败数据，回滚
            Double sss = Double.parseDouble("错误");
        }

        return true;
    }
    
    

    /**
     * *******************开始:读取评论***********************************
     */

    //获取评价内容主表
    @Override
    public Pscore queryPscore(String goodsId, String orderDetailId)
            throws Exception {
        Pscore pscore = new Pscore();
        if (Tools.isEmpty(goodsId) && Tools.isEmpty(orderDetailId)) {
            return null;
        } else {
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("goods_id", goodsId);
            map.put("order_detail_id", orderDetailId);
            pscore = (Pscore) dao.findForObject("ScoreDao.getScore", map);
        }
        return pscore;
    }
  //已经 打分项 (order_id :订单id,order_detail_id :订阅鲜花次数id)
    @Override
    public List<PscoreProject> queryScoreProjectOld(String goods_id, String order_detail_id)
            throws Exception {
        Map<String, Object> mapselect = new HashMap<>();
        mapselect.put("goods_id", goods_id);
        mapselect.put("order_detail_id", order_detail_id);
        List<PscoreProject> rmap = (List<PscoreProject>) dao.findForList("ScoreDao.getScoreProjectOld", mapselect);
        return rmap;
    }
    
  //获取评价内容（文字内容）
    @Override
    public List<PscoreText> queryScoreText(String goods_id, String orderDetailId)
            throws Exception {
        List<PscoreText> result = new ArrayList<>();
        List<PscoreText> pscoreText;
        if (Tools.isEmpty(goods_id) && Tools.isEmpty(orderDetailId)) {
            return null;
        } else {
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("goods_id", goods_id);
            map.put("order_detail_id", orderDetailId);
            pscoreText = (List<PscoreText>) dao.findForList("ScoreDao.getScoreText", map);
        }
        return pscoreText;
    }
    
    
  //获取评价图片
    @Override
    public List<Tpics> queryScoreTpics(String goods_id, String orderDetailId)
            throws Exception {
        List<Tpics> pscoreTpics = new ArrayList<Tpics>();
        if (Tools.isEmpty(goods_id) && Tools.isEmpty(orderDetailId)) {
            return null;
        } else {
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("goods_id", goods_id);
            map.put("order_detail_id", orderDetailId);
            pscoreTpics = (List<Tpics>) dao.findForList("ScoreDao.getScorePic", map);
        }
        return pscoreTpics;
    }
    
    /**
     * *******************结束:读取评论***********************************
     */
    
    
  //修改主表 是否有追平状态
    @Override
    public boolean updateScoreText(String scoreid, String additional) throws Exception {
        //评论文字表
        Integer j = 0;
        if (scoreid == null) {
            if (Tools.isEmpty(scoreid)) {
            } else {
                Map<String, Object> reutnmap = new HashMap<String, Object>();
                reutnmap.put("scoreid", scoreid);
                reutnmap.put("additional", additional);
                j = (Integer) dao.save("ScoreDao.updateOrderScore", reutnmap);
            }
        }
        return j > 0;
    }

}
